package com.wellsoft.mobileoa.utils.widget;

import android.app.AlertDialog;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.BaseWidget;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageField;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageFieldButton;
import com.wmobile.utils.ServiceResponse;

public class LinkWidget extends BaseWidget {
	private TextView fieldTitleTv;
	private Button fieldValueBtn;

	public LinkWidget(WSOA_PageField field, BaseView baseView) {
		this.field = field;
		this.baseView = baseView;
	}

	@Override
	public View getView() {
		view = LayoutInflater.from(baseView.getBaseActivity()).inflate(R.layout.linkwidget,
				null);
		fieldTitleTv = (TextView) view
				.findViewById(R.id.linkwidget_fieldtitletv);
		fieldTitleTv.setText(field.getFieldTitle());
		fieldValueBtn = (Button) view
				.findViewById(R.id.linkwidget_fieldvaluebtn);
		fieldValueBtn.setText(field.getFieldValue());
		fieldValueBtn.setOnClickListener(onClickListener);
		return view;
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			AlertDialog alertDialog = baseView.getBaseActivity().showWaitingDialog();
			WSOA_PageFieldButton button = field.getFieldButtons().get(0);
			ServiceResponse response = BaseAction.executeActions(button.getActionID(),
					button.getAction(), baseView);
			alertDialog.dismiss();
			if (!response.getExeFlag()) {
				baseView.getBaseActivity().showMessage(
						response.getMessage());
				return;
			}
		}
	};

}
